﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.DB;
using RMS.Common;

namespace RMS
{
    public partial class frmBarCodePrint : ICommonForm
    {
        private InventoryMng inventoryMng = null;
        private List<Inventory> listProductStore = null;
        private List<Inventory> listProductPrint = new List<Inventory>();

        public BarCodeCtrl barcodeDemo = new BarCodeCtrl();

        public frmBarCodePrint()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView2.AutoGenerateColumns = false;
                        
            BindDataToProductStore();            
        }

        private void BindDataToProductStore()
        {
            inventoryMng = new InventoryMng();
            listProductStore = new List<Inventory>();

            listProductStore = inventoryMng.GetAllInventory();
            if (listProductStore.Count < 1)
            {
                btnSelectAll.Enabled = btnUnSelect.Enabled = false;
            }


            dataGridView1.DataSource = listProductStore;
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<Inventory> newList = new List<Inventory>();
            newList.AddRange(listProductPrint);
            
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    var itemProductStore = listProductStore.Where(p => p.ID == row.Cells[1].Value.ToString()).SingleOrDefault();
                    
                    if (itemProductStore != null && !listProductPrint.Contains(itemProductStore))
                    {
                        listProductPrint.Add(itemProductStore);
                        newList.Add(itemProductStore);
                    }
                }
            }


            dataGridView2.DataSource = newList;
            
        }

     
        private void btnRemove_Click(object sender, EventArgs e)
        {
            List<Inventory> newList = new List<Inventory>();
            newList.AddRange(listProductPrint);

            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    var itemProductPrint = listProductPrint.Where(p => p.ID.Equals(row.Cells[1].Value)).SingleOrDefault();

                    if (itemProductPrint != null && listProductPrint.Contains(itemProductPrint))
                    {
                        listProductPrint.Remove(itemProductPrint);
                        newList.Remove(itemProductPrint);
                    }
                }
            }

            dataGridView2.DataSource = newList;
            
        }

        private void btnExportBarcode_Click(object sender, EventArgs e)
        {
            if (dataGridView2.RowCount > 0)
            {

            }
            else
            {
                MessageBox.Show("Hãy chọn sản phẩm trong kho rồi chuyển sang danh sách cần in !");
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            BarcodeTemplate barcodeTemplate = new BarcodeTemplate()
            {
                BarCodeHeight = (int)txtBarcodeHeight.Value,
                FooterFont = new Font("Arial", (Single)txtFooterFontSize.Value),
                HeaderFont = new Font("Arial", (Single)txtHeaderFontSize.Value),
                ShowFooter = chkShowFooter.Checked,
                ShowHeader = chkShowHeader.Checked,
                DefaultSize = new Size((int)txtImageWidth.Value, (int)txtImageHeight.Value),
                ShowPrice = chkPrintPrice.Checked,
                ShowProductName = chkPrintProductName.Checked,
                PrintAll = chkPrintAll.Checked,
                PrintCopyNumber = (int)txtNumberOfCopy.Value,
                
            };

            //Barcode Weight
            switch (txtBarcodeWeight.SelectedIndex)
            {
                case 1:
                    barcodeTemplate.Weight = BarCodeCtrl.BarCodeWeight.Medium;
                    break;
                case 2:
                    barcodeTemplate.Weight = BarCodeCtrl.BarCodeWeight.Large;
                    break;
                default:
                    barcodeTemplate.Weight = BarCodeCtrl.BarCodeWeight.Small;
                    break;
            }

            Form barcodePrintPreview = new DialogBarcodePrintPreview(barcodeTemplate, listProductPrint);
            barcodePrintPreview.ShowDialog();
        }

        private void chkShowHeader_CheckedChanged(object sender, EventArgs e)
        {
            chkPrintProductName.Enabled = chkPrintPrice.Enabled = txtHeaderFontSize.Enabled = chkShowHeader.Checked;

            if (!chkShowHeader.Checked)
            {
                chkPrintProductName.Checked = chkPrintPrice.Checked = false;
            }
            
        }

        private void chkShowFooter_CheckedChanged(object sender, EventArgs e)
        {
            txtFooterFontSize.Enabled = chkShowFooter.Checked;
                
            
        }

        private void chkPrintPrice_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void chkPrintAll_CheckedChanged(object sender, EventArgs e)
        {
            txtNumberOfCopy.Enabled = !chkPrintAll.Checked;            
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = true;
            }
        }

        private void btnUnSelect_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = false;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var strSearch = txtSearch.Text.Trim();

            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                BindDataToProductStore();
            }
            else
            {
                var newList = listProductStore.Where(p => p.ID.Contains(strSearch) || p.Name.Contains(strSearch) || p.Category.Contains(strSearch) || p.Description.Contains(strSearch) || p.InvoiceNo.Contains(strSearch) || p.ItemNo.Contains(strSearch) || p.MemoNo.Contains(strSearch)).ToList();
                dataGridView1.DataSource = newList;
                
            }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) btnSearch_Click(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}